// 配置基地址
axios.defaults.baseURL = 'https://hmajax.itheima.net'

// 封装轻提示
function showToast(msg) {
    const myToast = document.querySelector('.my-toast')
    const dialog = new bootstrap.Toast(myToast)
    document.querySelector('.toast-body').innerHTML = msg
    dialog.show()
}
function checkLogin() {
    const token = localStorage.getItem('token')
    if (!token) {
        setTimeout(function () {
            location.href = 'login.html'
        }, 2000)
    }
}

function logout() {
    const logout = document.querySelector('#logout')
    logout.addEventListener('click', function () {
        localStorage.removeItem('token')
        localStorage.removeItem('name')
        setTimeout(function () {
            location.href = 'login.html'
        }, 2000)
    })
}

axios.interceptors.request.use(function (config) {
    const token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = token
    }
    return config
}, function (err) {
    return Promise.reject(err)
})

axios.interceptors.response.use(function (response) {
    console.log('response:', response.data);
    return response.data
}, function (err) {
    console.dir(err);
    if (err.response.status === 401) {
        localStorage.removeItem('name')
        localStorage.removeItem('token')
        setTimeout(function () {
            location.href = 'login.html'
        })
    }

    return Promise.reject(err)
})


const clearfix = document.querySelector('.clearfix')
clearfix.addEventListener('click', function (e) {
    if (e.target.classList.contains('active')) {
        document.querySelector('.btn').classList.remove('active')
        e.target.classList.add('active')
        location.href = 'student.html'
    }
})